package io.kiki.sba.registry.server.clientInterface.mapper;


import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class ConnectionMapper {

    /**
     * <connectionIp:connectionPort, clientIp > *
     */
    private final ConcurrentHashMap<String, String> connectIdToClientIpMap = new ConcurrentHashMap<>();


    public void add(String connectId, String clientIp) {
        if (StringUtils.isNotEmpty(clientIp)) {
            connectIdToClientIpMap.putIfAbsent(connectId, clientIp);
        }
    }


    public void remove(String connectId) {
        connectIdToClientIpMap.remove(connectId);
    }


    public boolean contains(String connectId) {
        return connectIdToClientIpMap.containsKey(connectId);
    }


    public String get(String connectId) {
        return connectIdToClientIpMap.get(connectId);
    }


    public int size() {
        return connectIdToClientIpMap.size();
    }


    public Map<String, String> get() {
        return new HashMap<>(connectIdToClientIpMap);
    }
}
